<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客系统 - 文章列表</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .article-item {
            margin-bottom: 20px;
            padding-bottom: 20px;
            border-bottom: 1px solid #eee;
        }
        .article-title {
            margin-bottom: 5px;
        }
        .article-meta {
            color: #6c757d;
            font-size: 0.9rem;
            margin-bottom: 10px;
        }
        .article-excerpt {
            color: #495057;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }
        .pagination-container {
            text-align: center;
            margin-top: 30px;
        }
    </style>
</head>
<body>
<!-- 导航栏（复用之前的导航栏代码） -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <!-- 导航栏内容省略，与index.jsp相同 -->
</nav>

<!-- 主要内容区 -->
<div class="container mt-4">
    <div class="row">
        <!-- 左侧文章列表 -->
        <div class="col-md-8">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2>最新文章</h2>
                <c:if test="${loginUser != null}">
                    <a href="${pageContext.request.contextPath}/article/edit" class="btn btn-primary">
                        <i class="bi bi-plus-circle"></i> 发布文章
                    </a>
                </c:if>
            </div>

            <!-- 文章列表 -->
            <c:choose>
                <c:when test="${not empty articleList}">
                    <c:forEach items="${articleList}" var="article">
                        <div class="article-item">
                            <h3 class="article-title">
                                <a href="${pageContext.request.contextPath}/article/detail?id=${article.id}"
                                   class="text-decoration-none text-dark">
                                        ${article.title}
                                </a>
                            </h3>

                            <div class="article-meta">
                                <span class="me-3"><i class="bi bi-person"></i> ${article.authorName}</span>
                                <span class="me-3"><i class="bi bi-folder"></i> ${article.categoryName}</span>
                                <span class="me-3"><i class="bi bi-calendar"></i>
                                        <fmt:formatDate value="${article.createdTime}" pattern="yyyy-MM-dd HH:mm"/>
                                    </span>
                                <span><i class="bi bi-eye"></i> 阅读(${article.viewCount})</span>

                                <!-- 作者才能看到的编辑/删除按钮 -->
                                <c:if test="${loginUser != null && loginUser.id == article.userId}">
                                        <span class="ms-3">
                                            <a href="${pageContext.request.contextPath}/article/edit?id=${article.id}"
                                               class="text-primary">编辑</a>
                                        </span>
                                    <span>
                                            <a href="${pageContext.request.contextPath}/article/delete?id=${article.id}"
                                               class="text-danger"
                                               onclick="return confirm('确定要删除这篇文章吗？')">删除</a>
                                        </span>
                                </c:if>
                            </div>

                            <div class="article-excerpt">
                                    ${article.content}
                            </div>

                            <div class="mt-3">
                                <a href="${pageContext.request.contextPath}/article/detail?id=${article.id}"
                                   class="btn btn-sm btn-outline-secondary">阅读全文</a>
                            </div>
                        </div>
                    </c:forEach>
                </c:when>
                <c:otherwise>
                    <div class="alert alert-info">
                        暂无文章，快去发布你的第一篇文章吧！
                    </div>
                </c:otherwise>
            </c:choose>

            <!-- 分页控件 -->
            <div class="pagination-container">
                <nav aria-label="Page navigation">
                    <ul class="pagination justify-content-center">
                        <li class="page-item ${currentPage == 1 ? 'disabled' : ''}">
                            <a class="page-link" href="${pageContext.request.contextPath}/articles?page=${currentPage - 1}">上一页</a>
                        </li>

                        <c:forEach begin="1" end="${totalPage}" var="i">
                            <li class="page-item ${currentPage == i ? 'active' : ''}">
                                <a class="page-link" href="${pageContext.request.contextPath}/articles?page=${i}">${i}</a>
                            </li>
                        </c:forEach>

                        <li class="page-item ${currentPage == totalPage ? 'disabled' : ''}">
                            <a class="page-link" href="${pageContext.request.contextPath}/articles?page=${currentPage + 1}">下一页</a>
                        </li>
                    </ul>
                </nav>
                <div class="text-muted mt-2">
                    共 ${totalCount} 篇文章，当前第 ${currentPage}/${totalPage} 页
                </div>
            </div>
        </div>

        <!-- 右侧边栏 -->
        <div class="col-md-4">
            <!-- 搜索框 -->
            <div class="card mb-4">
                <div class="card-header">搜索</div>
                <div class="card-body">
                    <form action="${pageContext.request.contextPath}/search" method="get">
                        <div class="input-group">
                            <input type="text" name="keyword" class="form-control" placeholder="搜索文章...">
                            <button class="btn btn-primary" type="submit">搜索</button>
                        </div>
                    </form>
                </div>
            </div>

            <!-- 分类列表 -->
            <div class="card mb-4">
                <div class="card-header">文章分类</div>
                <ul class="list-group list-group-flush">
                    <c:forEach items="${categoryList}" var="category">
                        <li class="list-group-item d-flex justify-content-between align-items-center">
                            <a href="${pageContext.request.contextPath}/articles?category=${category.id}"
                               class="text-decoration-none">${category.name}</a>
                            <span class="badge bg-primary rounded-pill">${category.articleCount}</span>
                        </li>
                    </c:forEach>
                </ul>
            </div>
        </div>
    </div>
</div>

<!-- 引入Bootstrap和图标库 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">
</body>
</html>